﻿using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System;
using System.Text;

namespace ConsumerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var channel = Common.RabbitMQ.GetChannel();

            #region 主动拉取

            //var result = channel.BasicGet("mytest", true);

            //var msg = Encoding.UTF8.GetString(result.Body.ToArray());
            //Console.WriteLine(msg);

            #endregion

            //队列
            channel.QueueDeclare("mytest", true, false, false, null);

            #region 采用轮询方式获取

            var consumerEvent = new EventingBasicConsumer(channel);

            consumerEvent.Received += (sender, e) =>
            {
                var msg = Encoding.UTF8.GetString(e.Body.ToArray());
                Console.WriteLine(msg);
            };
            channel.BasicConsume("mytest", true, consumerEvent);

            #endregion


            Console.ReadKey();
        }

    }
}
